# 设置cmake最低版本号
cmake_minimum_required(VERSION 3.5) # CMake install : https://cmake.org/download/

# 设置工程名称
project(serialassist LANGUAGES CXX)
message("PROJECT_NAME: ${PROJECT_NAME}")

# Qt Kit Dir
if (WIN32)
    if (MINGW)
        message("GNU")
        set(CMAKE_PREFIX_PATH "D:/Qt/Qt5.14.2/5.14.2/mingw73_64") 
    elseif (MSVC)
        message("MSVC")
        set(CMAKE_PREFIX_PATH "C:/Qt/5.15.2/msvc2019_64")
        # Specify MSVC UTF-8 encoding 
        add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
        add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
    endif ()
elseif (APPLE)
    # Add Apple specific settings here
else ()
endif ()


# 查找Qt5包，需要设置Qt5_HOME环境变量
find_package(Qt5 COMPONENTS REQUIRED
    Core
    # Gui
    Widgets
    SerialPort
    # Multimedia
) # Qt COMPONENTS

# ==================== set build options ====================
# 【optional】设置工程包含当前目录，非必须
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 设置自动生成ui.h文件,AUTOUIC打开可以省去QT5_WRAP_UI命令
set(CMAKE_AUTOUIC ON)
# 设置自动生成moc文件,AUTOMOC打开可以省去QT5_WRAP_CPP命令
set(CMAKE_AUTOMOC ON)
# 【optional】设置自动处理.qrc文件
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)



# ==================== set Project Source Files ====================
set(SOURCE_FILES
        src/main.cpp
        src/serialassist.cpp
)
set(HEADER_FILES
        src/serialassist.h
)

# 添加资源文件
# SET(QT_RESOURCES img.qrc)

# set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/icon.rc")

# 如果禁用 CMAKE_AUTOUIC 变量，还可以使用 qt_wrap_ui() 该命令手动处理 .ui 文件。
# qt5_wrap_ui(UI_HEADERS mainwidget.ui)
# 如果禁用 CMAKE_AUTORCC 变量，还可以使用 qt5_add_resources() 命令手动处理 .qrc 文件。
# 同时可以 使用 AUTORCC_EXECUTABLE 变量指定 rcc 可执行文件的路径。
# qt5_add_resources(QT_RESOURCES pic.qrc)

add_executable(${PROJECT_NAME}
    # WIN32 # If you need a terminal for debug, please comment this statement 
    ${SOURCE_FILES}
    ${HEADER_FILES}
    # ${UI_HEADERS}
    ${QT_RESOURCES}
   
) 
target_link_libraries(${PROJECT_NAME} PRIVATE 
    Qt5::Widgets
    Qt5::SerialPort
    # Qt5::Gui
    # Qt5::Multimedia
    # Qt5::Widgets
) # Qt5 Shared Library

# create a GUI application
# if (CMAKE_BUILD_TYPE MATCHES "Release" OR CMAKE_CONFIGURATION_TYPES MATCHES "Release")
#     set_target_properties(${PROJECT_NAME} PROPERTIES
#             WIN32_EXECUTABLE ON
#             MACOSX_BUNDLE ON
#     )
# endif ()



# ==================== Deployment ====================
# Handle Windows deployment
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    message("WIN32 DEPLOYMENT")
    # 如果存在 windeployqt.exe 工具，则自动部署依赖项
    if (EXISTS "${CMAKE_PREFIX_PATH}/bin/windeployqt.exe")
        message("windeployqt exists")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND "${CMAKE_PREFIX_PATH}/bin/windeployqt.exe" "--compiler-runtime"
                "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.exe"
        )
    else ()
        message(FATAL_ERROR "windeployqt.exe not found!")
        # Set the debug suffix to copy the correct Qt libraries
        set(DEBUG_SUFFIX)
        if (CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_CONFIGURATION_TYPES MATCHES "Debug")
            set(DEBUG_SUFFIX "d")
        endif ()

        # Set the path to the Qt installation
        set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
            if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
                set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
            endif ()
        endif ()
        message("QT_INSTALL_PATH: ${QT_INSTALL_PATH}")

        # Copy the required Qt plugins to the build directory
        if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E make_directory
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        endif ()

        # Copy the required Qt libraries to the build directory
        foreach (QT_LIB Core Gui Widgets Multimedia Network Svg)
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy
                    "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                    "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
        endforeach ()

        # MINGW deployment, copy the required MinGW libraries
        if (MINGW)
            if (NOT DEFINED ENV{UCRT64})
                # message(FATAL_ERROR "UCRT64 environment variable not set!")
                set(MINGW_PATH "D:/Scoop/apps/msys2/current/ucrt64")
                message("MINGW ENV: ${MINGW_PATH}")
            else ()
                set(MINGW_PATH "$ENV{UCRT64}")
                message("MINGW UCRT64 ENV: ${MINGW_PATH}")
            endif ()
            add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                    COMMAND ${CMAKE_COMMAND} -E copy "${MINGW_PATH}/bin/libgcc_s_seh-1.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
                    COMMAND ${CMAKE_COMMAND} -E copy "${MINGW_PATH}/bin/libstdc++-6.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
                    COMMAND ${CMAKE_COMMAND} -E copy "${MINGW_PATH}/bin/libwinpthread-1.dll" "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
                    COMMENT "Deploying mingw runtime libraries from ${MINGW_PATH}/bin"
            )
        endif ()
    endif () # End of windeployqt.exe check
endif ()